// pages/order-detail/order-detail.js
const app = getApp();
import Toast from '../../component/toast/toast';
let _price = 0;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        payName: ['微信', '余额'],
        index: -1,
        isWxPay: true,
        id: '',
        couponArr:[],
        price:0
    },
    selectCoupon(e){
        let index = e.currentTarget.dataset.index;
        let str = `couponArr[${index}].isChecked`;
        this.setData({
            price: this.data.couponArr[index].isChecked ? _price : 0,
            [str] : !this.data.couponArr[index].isChecked,
            index: index,
        })
        // console.log(e.currentTarget.dataset.index);
    },
    // bindPickerChange(e) {
    //     // console.log('picker发送选择改变，携带值为', e.detail.value)
    //     this.setData({
    //         index: e.detail.value,
    //         isWxPay: e.detail.value == 0 ? true : false
    //     })
    // },
    getCouponList(){
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.getCouponList,
            data: {
                status: "0", 
                coupponType: "1", 
                userInfoId: app.userInfo.Id
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function(res) {
                console.log(res);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    // console.log(res.data.BodyContent);
                    let list = res.data.BodyContent;
                    if(list != null){
                        list.forEach(function (item, index) {
                            list[index].isChecked = false;
                        });
                    }
                    that.setData({
                        couponArr: list
                    });
                }
            },
            fail: function(res) {
                console.log(res);
            },
            complete: function(res) {
                wx.hideLoading();
            },
        })
    },
    commitPay() {
        wx.showLoading({
            title: '正在提交订单...',
            mask: true
        });
        let that = this;
        let data = {
            ServiceId: this.data.id,
            UserInfoId: app.userInfo.Id
        };
        if (this.data.couponArr != null && this.data.index != -1){
            if (this.data.couponArr[this.data.index].isChecked){
                data.CouponCode = this.data.couponArr[this.data.index].Code;
            }
        }
        console.log("url:" + app.globalData.serverAddress.payService + "data：" + JSON.stringify(data));
        wx.request({
            url: app.globalData.serverAddress.payFromService,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function(res) {
                console.log("---------->" , res);
                if (that.data.price <= 0) {
                    Toast("发布成功！");
                    wx.navigateBack({
                        delta: 5,
                    });
                    return;
                }
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    wx.requestPayment({
                        timeStamp: res.data.BodyContent.timeStamp,
                        nonceStr: res.data.BodyContent.nonceStr,
                        package: res.data.BodyContent.package,
                        signType: res.data.BodyContent.signType,
                        paySign: res.data.BodyContent.paySign,
                        success: function(res) {
                            console.log("success：" + JSON.stringify(res));
                            Toast("发布成功！");
                            wx.navigateBack({
                                delta: 5,
                            })
                        },
                        fail: function(res) {
                            console.log("fail" + JSON.stringify(res));
                        },
                        complete: function(res) {},
                    })
                }
                Toast(res.data.Message);
                console.log(res);
            },
            fail: function(res) {
                console.log("fail：" + JSON.stringify(res))
            },
            complete: function(res) {wx.hideLoading();},
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        let price = options.price;
        console.log(price);
        _price = price * 0.02;
        if(_price < 0.1){
            _price = 0.1;
        }
        this.setData({
            id: options.id,
            price: _price
        });
        // let that = this;
        this.getCouponList();
        // setTimeout(function(){
        //     that.getCouponList();
        // },2000)
        // this.getCouponList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})